/*
 * User: rant
 * Date: Sep 26, 2007
 *
 *
 */
package laMalinche.rete.rule;

import laMalinche.rete.nodes.editing.FutureEditingInformer;
import laMalinche.rete.nodes.ConditionNode;
import laMalinche.rete.ConditionNodeFactory;
import laMalinche.Pair;
import laMalinche.Adapter;
import laMalinche.DevMode;

import java.util.*;

import com.google.inject.Inject;

/**
 *
 */
public class WillBeActiveProductionRuleVisitor implements ProductionRuleVisitor {
    @Inject
    ConditionNodeFactory conditionNodeFactory;

    private final Set<String> willBeActiveForRule = new HashSet<String>();
	private final Set<String> inEditing =  new HashSet<String>();

	public WillBeActiveProductionRuleVisitor(final Set<String> inEditing) {
        DevMode.getInstance().getInjector().injectMembers(this);
        this.inEditing.addAll(inEditing);
	}

	public Set<String> getWillBeActiveForRule() {
		return willBeActiveForRule;
	}

	public void visitProductionRule(final ProductionRule rule) {
		final Map<Pair<?, BusinessRuleType>, ConditionNode> condiMap = conditionNodeFactory.getCondiMap();
		Set<FutureEditingInformer> informers = new HashSet<FutureEditingInformer>();
		final List<Condition> conditionList = rule.getConditions();
		final BusinessRuleType ruleType = rule.getProductionRuleType();
		for (Condition condition : conditionList) {
			final ConditionNode conditionNode = condiMap.get(new Pair<Object, BusinessRuleType>(new Adapter(condition), ruleType));
			final FutureEditingInformer informer = (FutureEditingInformer) condiMap.get(new Pair<ConditionNode, BusinessRuleType>(conditionNode, ruleType));
			informers.add(informer);
		}
		final ConditionNode conditionNode = condiMap.get(new Pair<Action, BusinessRuleType>(rule.getAction(), ruleType));
		final FutureEditingInformer informer = (FutureEditingInformer) condiMap.get(new Pair<ConditionNode, BusinessRuleType>(conditionNode, ruleType));
		informers.add(informer);
		for (FutureEditingInformer futureEditingInformer : informers) {
						if (futureEditingInformer.willBeActive(inEditing)) {
							willBeActiveForRule.addAll(futureEditingInformer.activatedBy(inEditing));
						} else {
							willBeActiveForRule.clear();
							break;
						}
		}
//		return informers;
	}


}